package com.jianyun.jyzs.utils;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.content.Context;
import android.widget.DatePicker;
import android.widget.TextView;

import java.util.Calendar;

public class DateDialogUtil {
	// 日期控件
	private Calendar cdar = Calendar.getInstance();
	private int mYear = cdar.get(Calendar.YEAR);
	private int mMonth = cdar.get(Calendar.MONTH);
	private int mDay = cdar.get(Calendar.DATE);
	private DatePickerDialog picker; // 日期dialog控件
	private OnDateSetListener mDate;
	private TextView et;

	public DateDialogUtil(Context ctx, TextView et) {
		this.et = et;
		String[] tempDate = null;
		mDate = new MyOnDateSetListener();
		if(et != null && et.getText() != null && !et.getText().toString().trim().equals("")){
			try {
				tempDate = et.getText().toString().split("-");
				mYear = Integer.valueOf(tempDate[0]);
				mMonth = Integer.valueOf(tempDate[1])-1;
				mDay = Integer.valueOf(tempDate[2]);
			} catch (Exception e) {
				mYear = cdar.get(Calendar.YEAR);
				mMonth = cdar.get(Calendar.MONTH);
				mDay = cdar.get(Calendar.DATE);
			}
		}
		picker = new DatePickerDialog(ctx, mDate, mYear, mMonth, mDay);

	}
	public DateDialogUtil(Context ctx, TextView et , OnDateSetListener myOnDateSetListener) {
		this.et = et;
		String[] tempDate = null;
		mDate = myOnDateSetListener;
		if(et != null && et.getText() != null && !et.getText().toString().trim().equals("")){
			try {
				tempDate = et.getText().toString().split("-");
				mYear = Integer.valueOf(tempDate[0]);
				mMonth = Integer.valueOf(tempDate[1])-1;
				mDay = Integer.valueOf(tempDate[2]);
			} catch (Exception e) {
				mYear = cdar.get(Calendar.YEAR);
				mMonth = cdar.get(Calendar.MONTH);
				mDay = cdar.get(Calendar.DATE);
			}
		}
		picker = new DatePickerDialog(ctx, mDate, mYear, mMonth, mDay);

	}
	//限制最大时间
	public DateDialogUtil(Context ctx, TextView et, long data) {
		this.et = et;
		String[] tempDate = null;
		mDate = new MyOnDateSetListener();
		if(et != null && et.getText() != null && !et.getText().toString().trim().equals("")){
			try {
				tempDate = et.getText().toString().split("-");
				mYear = Integer.valueOf(tempDate[0]);
				mMonth = Integer.valueOf(tempDate[1])-1;
				mDay = Integer.valueOf(tempDate[2]);
			} catch (Exception e) {
				mYear = cdar.get(Calendar.YEAR);
				mMonth = cdar.get(Calendar.MONTH);
				mDay = cdar.get(Calendar.DATE);
			}
		}
		picker = new DatePickerDialog(ctx, mDate, mYear, mMonth, mDay);
		DatePicker datePicker = picker.getDatePicker();
		datePicker.setMaxDate(data);

	}
	//限制最小时间
	public DateDialogUtil(Context ctx, TextView et, long data, long data2) {
		this.et = et;
		String[] tempDate = null;
		mDate = new MyOnDateSetListener();
		if(et != null && et.getText() != null && !et.getText().toString().trim().equals("")){
			try {
				tempDate = et.getText().toString().split("-");
				mYear = Integer.valueOf(tempDate[0]);
				mMonth = Integer.valueOf(tempDate[1])-1;
				mDay = Integer.valueOf(tempDate[2]);
			} catch (Exception e) {
				mYear = cdar.get(Calendar.YEAR);
				mMonth = cdar.get(Calendar.MONTH);
				mDay = cdar.get(Calendar.DATE);
			}
		}
		picker = new DatePickerDialog(ctx, mDate, mYear, mMonth, mDay);
		DatePicker datePicker = picker.getDatePicker();
		datePicker.setMinDate(data2);

	}

	/**
	 * 创建要显示的对话框
	 */
	public Dialog onCreateDialog() {

		return picker;

	}

	/**
	 * 日期控件
	 * 
	 * @author Administrator
	 * 
	 */
	public class MyOnDateSetListener implements OnDateSetListener {

		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			String startDate = "";
			String month = monthOfYear + 1 + "";
			if (monthOfYear + 1 < 10) {
				month = "0" + month;
			}
			String day = dayOfMonth + "";
			if (dayOfMonth < 10) {
				day = "0" + day;
			}

			startDate = year + "-" + month + "-" + day;
			et.setText(startDate);
		}

	}


}
